package com.itxiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itxiao.dao.CategoryDao;
import com.itxiao.entity.Category;
import com.itxiao.entity.SysMenu;
import com.itxiao.entity.SysRoleMenu;
import com.itxiao.entity.SysUserRole;
import com.itxiao.pojo.bo.Page;
import com.itxiao.pojo.vo.MenuTreeVo;
import com.itxiao.pojo.vo.MetaBean;
import com.itxiao.pojo.vo.R;
import com.itxiao.pojo.vo.spu.CategoryVo;
import com.itxiao.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2025-10-12 17:07:25
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
   @Resource CategoryDao categoryDao;
    @Override
    public R queryList(Page page) {
        List<Category> categoryList = categoryDao.selectList(
                new QueryWrapper<Category>().eq("category_type", page.getCategoryType()));
        if (categoryList.isEmpty()) {
            return R.ok(Collections.emptyList());
        }

        // 5. 构建并返回路由树
        List<CategoryVo> routerTree = buildCategoryTree(categoryList);
        Map<String, Object> data = new HashMap<>();
        data.put("list", routerTree);
        return R.ok(data);

    }

    @Override
    public R add(Category category) {
        int insert = categoryDao.insert(category);
        if (insert  == 0) {
            return R.fail("插入失败");
        }
        return R.ok();
    }

    @Override
    public R updateInfo(Category category) {
        int insert = categoryDao.updateById(category);
        if (insert  == 0) {
            return R.fail("修改失败");
        }
        return R.ok();
    }

    @Override
    public R del(List<Integer> ids) {
        return R.ok(categoryDao.deleteBatchIds(ids));
    }

    private CategoryVo convertToCategoryVo(Category category) {
        CategoryVo vo = new CategoryVo();
        vo.setId(category.getId());
        vo.setName(category.getName());
        vo.setParentId(category.getParentId());
        vo.setIcon(category.getIcon());
        vo.setSort(category.getSort());
        vo.setCategoryType(category.getCategoryType());
        vo.setPcImage(category.getPcImage());
        vo.setLevel(category.getLevel());

        vo.setList(new ArrayList<>());
        return vo;
    }
    private List<CategoryVo> buildCategoryTree(List<Category> categoryList) {
        if (categoryList == null || categoryList.isEmpty()) {
            return Collections.emptyList();
        }

        // 1. 先转换所有 Category 为 CategoryVo，并用 Map 存储 id -> vo
        Map<Integer, CategoryVo> voMap = new HashMap<>();
        for (Category category : categoryList) {
            CategoryVo vo = convertToCategoryVo(category);
            voMap.put(category.getId(), vo);
        }

        // 2. 构建父子关系
        List<CategoryVo> rootList = new ArrayList<>();

        for (Category category : categoryList) {
            CategoryVo currentVo = voMap.get(category.getId());
            Integer parentId = category.getParentId();

            if (parentId == null || parentId == 0) {
                // 是根节点，加入根列表
                rootList.add(currentVo);
            } else {
                // 是子节点，找到父节点并加入 children
                CategoryVo parentVo = voMap.get(parentId);
                if (parentVo != null) {
                    parentVo.getList().add(currentVo);
                }
            }
        }

        // 3. 根节点按 sort 排序（可选）
        rootList.sort(Comparator.comparing(CategoryVo::getSort));

        return rootList;
    }
}

